package com.ruoyi.util;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import com.ruoyi.common.utils.StringUtils;

import java.util.Locale;
import java.util.regex.Pattern;

public class PhoneNumberUtil {
    //找出直辖市
    private final static String[] MUNICIPALITY = { "北京市", "天津市", "上海市", "重庆市" };
    //找出自治区
    private final static String[] AUTONOMOUS_REGION = { "新疆", "内蒙古", "西藏","宁夏", "广西" };

    private static com.google.i18n.phonenumbers.PhoneNumberUtil phoneNumberUtil = com.google.i18n.phonenumbers.PhoneNumberUtil
            .getInstance();

    //提供与电话号码相关的运营商信息
    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper
            .getInstance();

    // 提供与电话号码有关的地理信息
    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder
            .getInstance();

    //中国大陆区号
    private final static int COUNTRY_CODE = 86;

    /**
     * 根据手机号 判断手机号是否有效
     * @param phoneNumber
     * @return true-有效 false-无效
     */
    public static boolean checkPhoneNumber(String phoneNumber) {
        if(StringUtils.isEmpty(phoneNumber)){
            return false;
        }
        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);

        return phoneNumberUtil.isValidNumber(pn);

    }

    /**
     * 根据手机号 判断手机运营商
     * @param phoneNumber
     */
    public static String getCarrier(String phoneNumber) {

        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        // 返回结果只有英文，自己转成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        switch (carrierEn) {
            case "China Mobile":
                carrierZh += "移动";
                break;
            case "China Unicom":
                carrierZh += "联通";
                break;
            case "China Telecom":
                carrierZh += "电信";
                break;
            default:
                break;
        }
        return carrierZh;
    }

    /**
     * 根据手机号 获取手机归属地
     */
    public static String getGeo(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
    }

    /**
     * 根据手机号 获取封裝信息
     */
    public static PhoneModel getPhoneModel(String phoneNumber) {

        if (checkPhoneNumber(phoneNumber)) {
            String geo = getGeo(phoneNumber);
            PhoneModel phoneModel = new PhoneModel();
            String carrier = getCarrier(phoneNumber);
            phoneModel.setCarrier(carrier);
            // 直辖市
            for (String val : MUNICIPALITY) {
                if (geo.equals(val)) {
                    phoneModel.setProvinceName(val.replace("市", ""));
                    phoneModel.setCityName(val);
                    return phoneModel;
                }
            }
            // 自治区
            for (String val : AUTONOMOUS_REGION) {
                if (geo.startsWith(val)) {
                    phoneModel.setProvinceName(val);
                    phoneModel.setCityName(geo.replace(val, ""));
                    return phoneModel;
                }
            }

            // 其它
            String[] splitArr = geo.split("省");
            if (splitArr != null && splitArr.length == 2) {
                phoneModel.setProvinceName(splitArr[0]);
                phoneModel.setCityName(splitArr[1]);
                return phoneModel;
            }
        }
        return null;
    }

    /**
     * 判断是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) { //可以正常使用

        Pattern pattern = Pattern.compile("[0-9]*");

        return pattern.matcher(str).matches();

    }

    /**
     * 过滤掉手机号中的特殊字符：空格，‘-’等等字符。
     *
     * @param phone : "158-8922-2222"  , "158 8955 6336"
     * @return :phone : "15889222222"  , "15889556336"
     */
    public static String replaceSpaceStr2(String phone) {
        if (StringUtils.isBlank(phone)){
            return "";
        }
        StringBuffer sb = new StringBuffer();
//        CharUpperLowerChange.isNumeric(phone); //判断是否是数字
        int length = phone.length();
        for (int i = 0; i < length; i++) {
            String subStr = phone.substring(i, i + 1);
            if (isNumeric(subStr)) {  //如果这个字符是数字，则保存在sb中。
                sb.append(subStr);
            } else {
                continue;
            }
        }
        return sb.toString();
    }
}
